package v1

import (
	"github.com/gin-gonic/gin"
	"perfect-admin/global"
	"perfect-admin/global/response"
	"perfect-admin/model"
	"perfect-admin/model/request"
	"perfect-admin/service"
	"perfect-admin/utils"
)

func GetOperLogList(c *gin.Context)  {
	var queryInfo request.QueryInfo
	if err := c.ShouldBind(&queryInfo); err != nil {
		response.ResponseError(c, 201, err.Error())
		return
	}
	err, total, logList := service.GetOperLogList(queryInfo)
	if err != nil {
		response.ResponseError(c, 202, err.Error())
		return
	}
	response.ResponseSuccessWithData(c, "获取操作日志列表成功", map[string]interface{}{
		"total": total,
		"list": logList,
	})
	return
}

func GetOperLogListByIds(c *gin.Context)  {
	var ids_str []string
	ids_str, ok := c.GetQueryArray("ids[]")
	if !ok {
		response.ResponseError(c, 201, "参数解析有误")
		return
	}
	var ids []int
	for _, id := range ids_str {
		x, _ := utils.StringToInt(id)
		ids = append(ids, x)
	}
	logList, err := service.GetOperLogByIds(ids)
	if err != nil {
		response.ResponseError(c, 500, "获取失败")
		global.LOG.Error(err)
		return
	}
	response.ResponseSuccessWithData(c, "获取成功", logList)
	return
}

func DeleteOperLog(c *gin.Context) {
	var ids_str []string
	ids_str, ok := c.GetQueryArray("ids[]")
	if !ok {
		response.ResponseError(c, 201, "参数解析有误")
		return
	}
	var data model.SysOperLog
	data.UpdateBy = string(utils.GetClaims(c).UserID)
	var ids []int
	for _, id := range ids_str {
		x, _ := utils.StringToInt(id)
		ids = append(ids, x)
	}
	err := service.BatchDeleteOperLog(ids)
	if err != nil {
		response.ResponseError(c, 500, "修改失败")
		global.LOG.Error(err)
		return
	}
	response.ResponseSuccess(c, "删除成功")
	return
}


func CleanOperLog(c *gin.Context)  {
	if err := service.CleanOperLog(); err != nil{
		response.ResponseError(c, 201, "清空失败")
		global.LOG.Error(err)
		return
	}
	response.ResponseSuccess(c, "清空成功")
	return
}